package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;
    @Autowired
    private StudentService studentService;
    /**
     * 班级分页条件查询
     *
     * @param name
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<Clazz> findPage(String name, LocalDate begin, LocalDate end, int page, int pageSize) {

        IPage<Clazz> iPage=new Page<Clazz>(page,pageSize);
        IPage<Clazz> page1 = this.page(iPage, Wrappers.<Clazz>lambdaQuery()
                .like(name!=null,Clazz::getName,name)
                .between(begin!=null&&end!=null,Clazz::getBeginDate,begin,end)
                .orderByDesc(Clazz::getUpdateTime));
        List<Clazz> records = page1.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            for (Clazz record : records) {
                record.setMasterName(empService.getById(record.getMasterId()).getName());
                if (record.getBeginDate().isAfter(LocalDate.now())){
                    record.setStatus("未开班");
                }
                if (record.getEndDate().isAfter(LocalDate.now())){
                    record.setStatus("在读");
                }else record.setStatus("已结课");
            }
        }

        return new PageResult<>(page1.getTotal(),page1.getRecords());
    }

    /**
     * 新增班级
     *
     * @param clazz
     */
    @Override
    public void SaveClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazz.setCreateTime(LocalDateTime.now());
        this.save(clazz);
    }

    /**
     * 查询回显
     *
     * @param id
     * @return
     */
    @Override
    public Clazz selectById(Integer id) {
        Clazz clazz = this.getById(id);

        return clazz;
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void updateClazzById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        this.updateById(clazz);
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {
        if (studentService.exists(Wrappers.<Student>lambdaQuery().eq(Student::getClazzId,id))){
            return false;
        }
        this.removeById(id);
        return true;
    }
}
